/**
 * Created with IntelliJ IDEA.
 * User: nico
 * Date: 8/26/12
 * Time: 9:47 PM
 * To change this template use File | Settings | File Templates.
 */

$(document).ready(function(){
    doubleLabel();
    checkIsDoctor();
    $("#otherDegree").parent().parent().hide();
    showHideCuit();
    showMoreInfo($("#div_"), "imgInfo");
    showMoreInfo($("#div_2"), "imgInfo2");
});

function doubleLabel(){
    var row = $('#areas').parent().parent().clone()[0];
    row.children[0].children[0].innerText = 'Specialty:';
    var elem = row.children[1].children[0];
    var elem2 = row.children[1].children[1];
    var specialities = row.children[1].children[2];
    var specialityId = $($('#areas')[0].parentNode.parentNode.children[1].children[2]).val();
    var elem3 = row.children[1].children[3];
    elem.parentNode.removeChild(elem);
    elem2.parentNode.removeChild(elem2);
    elem3.parentNode.removeChild(elem3);
    $('#areas').parent().parent().after(row);
    $($('#areas')[0].parentNode.parentNode.children[1].children[2]).remove();
    $(specialities).val(specialityId);
    areasTemp = specialities;
}

function checkIsDoctor(){
    if($('#isDoctor').attr('checked')){
        $('#centro').parent().parent().hide();
        $('#degree').parent().parent().show();
    }else{
        $('#centro').parent().parent().show();
        $('#degree').parent().parent().hide();
    }
}

function validaCuit(cuit) {
    if (typeof (cuit) == 'undefined')
        return true;
    cuit = cuit.toString().replace(/[-_]/g, "");
    if (cuit == '')
        return true; //No estamos validando si el campo esta vacio, eso queda para el "required"
    if (cuit.length != 11)
        return false;
    else {
        var mult = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
        var total = 0;
        for (var i = 0; i < mult.length; i++) {
            total += parseInt(cuit[i]) * mult[i];
        }
        var mod = total % 11;
        var digito = mod == 0 ? 0 : mod == 1 ? 9 : 11 - mod;
    }
    return digito == parseInt(cuit[10]);
}

function validateInvestigator() {
    jQuery.validator.addMethod("cuit", validaCuit, 'Please enter a valid CUIT/CUIL');

    jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
        phone_number = phone_number.replace(/\s+/g, "");
        return this.optional(element) || phone_number.length > 9 &&
            phone_number.match(/^\d{12}$/);
    }, "Please enter a valid phone number");

    jQuery.validator.addMethod("valueNotEquals", function(value, element, arg){
        return value != arg;
    });

    $("#formEdit")
        .validate(
        {
            rules : {
                'nombre' : "required",
                'apellido' : "required",
                'email' : {
                    required: true
                },
                'compromisopais.id' : {
                    required : true,
                    valueNotEquals: "-1"
                },
                'data': {
                    accept: "doc|docx|pdf"
                }
            },
            messages:{
                 'data': "Only permitted doc, docx, or pdf.",
                 'compromisopais.id' : "*"
            }
        })

}

function isOtherDegree(value){
    if (value == -2){
        $("#otherDegree").parent().parent().show()
    }
    else{
        $("#otherDegree").parent().parent().hide()

    }
}

function beforeSaveInvestigator(){

    if ($('#degree option:selected').val() == -2){
        $("#degreeDescripcion").val($("#otherDegree").val());
    }

    if ($('#degree option:selected').val() == -1){
        $("#degreeDescripcion").val('');
    }
}

function showHideCuit(){
    if($('#country option:selected').val() == -1){
        hideAllCuit();
        return;
    }

    var elegido = $('#country option:selected').text();
    if ( elegido == "Argentina"){
        showCuit();
    }
    else if( elegido == "Brazil"){
        showCnpj();
    }
    else if (elegido == "Colombia"){
        showNit();
    }
    else if (elegido == "Chile" || elegido == "Peru"){
        showRut();
    }
}

function hideAllCuit(){
    $("#cuit").parent().parent().hide();
    $("#cnpj").parent().parent().hide();
    $("#nit").parent().parent().hide();
    $("#rut").parent().parent().hide();

    $("#cuit").val('');
    $("#cnpj").val('');
    $("#nit").val('');
    $("#rut").val('');
}

function showCuit(){
    $("#cuit").parent().parent().show();
    $("#cnpj").parent().parent().hide();
    $("#nit").parent().parent().hide();
    $("#rut").parent().parent().hide();

    $("#cnpj").val('');
    $("#nit").val('');
    $("#rut").val('');
}

function showCnpj(){
    $("#cuit").parent().parent().hide();
    $("#cnpj").parent().parent().show();
    $("#nit").parent().parent().hide();
    $("#rut").parent().parent().hide();

    $("#cuit").val('');
    $("#nit").val('');
    $("#rut").val('');
}

function showNit(){
    $("#cuit").parent().parent().hide();
    $("#cnpj").parent().parent().hide();
    $("#nit").parent().parent().show();
    $("#rut").parent().parent().hide();

    $("#cuit").val('');
    $("#cnpj").val('');
    $("#rut").val('');
}

function showRut(){
    $("#cuit").parent().parent().hide();
    $("#cnpj").parent().parent().hide();
    $("#nit").parent().parent().hide();
    $("#rut").parent().parent().show();

    $("#cuit").val('');
    $("#cnpj").val('');
    $("#nit").val('');
}